package com.javandad.app.client.chat;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.javandad.app.client.AsyncCallback;
import com.javandad.app.client.DialogBox;
import com.javandad.app.client.cells.ChatMessageCell;
import com.javandad.app.shared.entity.ChatMessage;
import com.javandad.app.shared.entity.User;

public class ChatWindow extends DialogBox {

	private User partner;
	private ChatServiceAsync chatService;

	private final List<ChatMessage> messages = new ArrayList<ChatMessage>();
	private CellList<ChatMessage> messageList;
	private ScrollPanel messagePanel;
	private final TextBox text = new TextBox();
	private final Button send = new Button("send");

	public ChatWindow(User partner, ChatServiceAsync chatService) {
		super(partner.toString());
		this.partner = partner;
		this.chatService = chatService;
		initGui();
	}

	private void initGui() {
		VerticalPanel main = new VerticalPanel();
		messageList = new CellList<ChatMessage>(new ChatMessageCell(partner));
		messagePanel = new ScrollPanel(messageList);
		// messagePanel.add(messageList);
		messagePanel.setSize("300px", "300px");
		main.add(messagePanel);

		HorizontalPanel typo = new HorizontalPanel();
		class TypoHandler implements KeyPressHandler, ClickHandler {
			@Override
			public void onClick(ClickEvent event) {
				sendText();
			}
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
					sendText();
			}
			private void sendText()
			{
				sendMessage(text.getText());
				text.setText("");
				text.setFocus(true);
			}
		}
		TypoHandler typoHandler = new TypoHandler();
		text.addKeyPressHandler(typoHandler);
		text.setWidth("100%");
		typo.add(text);
		send.addClickHandler(typoHandler);
		typo.add(send);
		typo.setWidth("300px");
		main.add(typo);

		super.getDisplay().add(main);
		// super.dlg.setWidth("500");
		// super.dlg.setHeight("300");
	}

	public User getPartner() {
		return partner;
	}

	private final Timer delay = new Timer() {
		@Override
		public void run() {
			messagePanel.scrollToBottom();
			text.setFocus(true);
		}
	};

	public void newMessage(ChatMessage m) {
		messages.add(m);
		messageList.setRowData(messages);
		delay.schedule(100);
	}

	public void sendMessage(String text) {
		chatService.send(partner.getUserId(), text, new AsyncCallback<ChatMessage>() {
			@Override
			public void onSuccess(ChatMessage result) {
				newMessage(result);
			}
		});
	}
	
	@Override
	public void show() {
		super.show();
		messagePanel.scrollToBottom();
	}
}
